package concole;

import java.util.ArrayList;

public class Course {
	private String courseNumber;
	private int startDate = 1;
	private int endDate = 17;
	private String courseName;
	private int classTotalHour = 30;
	private int credit = 2;
	private int sumOfTeacher = 1;
	private boolean isCompulsory = true;
	private String place = "remainUnlnown";
	private String time = "unknown"; // Ex:Tues1-2
	private String teachersNumString = "unknown";
	private String assistenceNameString = "unknowm";
	public String teacherNameString = "Unknowm";
	public ArrayList<Student> assistants = new ArrayList<Student>();
	public ArrayList<Teacher> teachers = new ArrayList<Teacher>();

	public Course() {
		teachers.add(new Teacher("000"));
		assistants.add(new Student("000"));
	}

	public Course(String courseNum, String teacherNumberString,
			String courseName, String teacherNameString) {
		if (teacherNumberString.contains("&")) {
			String[] numStr = teacherNumberString.split("&");
			String[] nameStr = teacherNameString.split("&");

			for (int i = 0; i < numStr.length; i++) {
				teachers.add(new Teacher(numStr[i], nameStr[i]));
			}
		} else {
			teachers.add(new Teacher(teacherNumberString, teacherNameString));
		}
		this.courseNumber = courseNum;
		this.courseName = courseName;
	}

	public Course(String courseStringFromFile) {
		String[] strings = courseStringFromFile.split(" ");
		this.courseNumber = strings[0];
		this.courseName = strings[1];
		this.teacherNameString = strings[2];
		this.teachersNumString = strings[3];
		this.isCompulsory = (strings[4].equals("true"));
		this.classTotalHour = Integer.parseInt(strings[5]);
		this.credit = Integer.parseInt(strings[6]);
		this.startDate = Integer.parseInt(strings[7]);
		this.endDate = Integer.parseInt(strings[8]);
		this.time = strings[9];
		this.place = strings[10];
		this.assistenceNameString = strings[11];
		this.assistants.add(new Student("000"));
		if (strings[3].contains("&")) {
			String[] numStr = strings[3].split("&");
			String[] nameStr = teacherNameString.split("&");

			for (int i = 0; i < numStr.length; i++) {
				numStr[i] = numStr[i].trim();
				nameStr[i] = nameStr[i].trim();
				//teachers.add(new Teacher(numStr[i], nameStr[i]));
				teachers.add(new Teacher(numStr[i]));
			}
		} else {
			//teachers.add(new Teacher(strings[3], teacherNameString));
			teachers.add(new Teacher(strings[3]));
		}
		if (assistenceNameString.matches("[0-9]*")) {
			System.out
					.println("assisteneName now is number is going to be transformed");
			if (assistenceNameString.contains("&")) {
				String[] assisNumStrings = assistenceNameString.trim().split(
						"&");
				for (int i = 0; i < assisNumStrings.length; i++) {
					this.addAssistent(new Student(assisNumStrings[i]));
				}
			} else {
				this.addAssistent(new Student(assistenceNameString));
			}
		}
	}
	public Course(String courseStinrgFromFile,boolean isRemote){
		String[] strings = courseStinrgFromFile.split(" ");
		this.courseNumber = strings[0];
		this.courseName = strings[1];
		this.teacherNameString = strings[2];
		this.teachersNumString = strings[3];
		this.isCompulsory = (strings[4].equals("true"));
		this.classTotalHour = Integer.parseInt(strings[5]);
		this.credit = Integer.parseInt(strings[6]);
		this.startDate = Integer.parseInt(strings[7]);
		this.endDate = Integer.parseInt(strings[8]);
		this.time = strings[9];
		this.place = strings[10];
		this.assistenceNameString = strings[11];
		System.out.println(this.toTransferString());
		
	}

	/**
	 * getter and setter
	 * 
	 * @return
	 */
	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String nameString) {
		this.courseName = nameString;
	}

	public String getCourseNumber() {
		return courseNumber;
	}

	public int getCredit() {
		return this.credit;
	}

	public void setCredit(int c) {
		this.credit = c;
	}

	public int getTotalHour() {
		return this.classTotalHour;
	}

	public void setTotalHour(int hour) {
		this.classTotalHour = hour;
	}

	public boolean isCompulsory() {
		return this.isCompulsory;
	}

	public void setProperity(boolean isCompulsory) {
		this.isCompulsory = isCompulsory;
	}

	public int getProperity() {
		return (this.isCompulsory) ? 1 : 0;
	}

	public void addAssistent(Student s) {
		if (this.assistants.get(0).getNum().equals("000")) {
			this.assistants.clear();
			this.assistenceNameString = "";
		}
		this.assistants.add(s);
		this.assistenceNameString += s.getName();

	}

	public void setCourseNumber(String courseNumber) {
		this.courseNumber = courseNumber;
	}

	public String getPlace() {
		return this.place;
	}

	public void setPlace(String s) {
		this.place = s;
	}

	public String getTime() {
		return this.time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getStartDate() {
		return startDate;
	}

	public void setStartDate(int startDate) {
		this.startDate = startDate;
	}

	public int getEndDate() {
		return endDate;
	}

	public void setEndDate(int endDate) {
		this.endDate = endDate;
	}

	public String getTeacherNameString() {
		StringBuffer nameStr = new StringBuffer();
		if(teachers.size()==0){
			return this.teacherNameString;
		}
		for (int i = 0; i < this.teachers.size(); i++) {
			nameStr.append(this.teachers.get(i).getName());
			nameStr.append("&");

		}
		nameStr.deleteCharAt(nameStr.lastIndexOf("&"));
		return nameStr.toString();
	}

	public void setTeacherNameString(String teachersNameString) {
		this.teacherNameString = teachersNameString;
		System.out.println(this.teacherNameString);
	}

	public void SetTeacherNumString(String teacherNumString) {
		this.teachersNumString = teacherNumString;
		String[] strings = teacherNumString.split("&");
		this.setSumOfTeachers(strings.length);
		this.teachers.clear();
		for (int i = 0; i < this.getSumOfTeachers(); i++) {
			this.teachers.add(new Teacher(strings[i]));
		}

	}

	public int getSumOfTeachers() {
		return this.sumOfTeacher;
	}

	public void setSumOfTeachers(int sum) {
		this.sumOfTeacher = sum;
	}

	public String getTeacherNumString() {
		StringBuilder numStr = new StringBuilder();
		if(teachers.size()==0){
			return this.teachersNumString;
		}
		for (int i = 0; i < this.teachers.size(); i++) {
			numStr.append(this.teachers.get(i).getNum());
			numStr.append("&");
		}
		System.out.println(numStr.toString());
		numStr.deleteCharAt(numStr.lastIndexOf("&"));
		return numStr.toString();
	}

	public void setTeacherNumString(String s) {
		this.teachersNumString = s;
	}

	public String toShowString() {
		String[] toShow = this.toStoreString().split(" ");
		return "course number: " + toShow[0] + " course name" + toShow[1]
				+ " Teached by:" + toShow[2] + "(" + toShow[3] + ")\n"
				+ (isCompulsory ? " Compulsory Course" : " Optional Course")
				+ "    Total course hour is:" + toShow[5]
				+ "    Credit Of the course:" + toShow[6] + "\nFrome week "
				+ toShow[7] + " to week" + toShow[8] + "   Taken time:"
				+ toShow[9] + " every week " + "    Place: " + toShow[10]
				+ "\nAssistents: " + toShow[11];
	}

	public String toStoreString() {
		String nameStr = this.getTeacherNameString();
		String numStr = this.getTeacherNumString();
		return this.courseNumber + " " + this.courseName + " "
				+ nameStr.toString() + " " + numStr.toString() + " "
				+ this.isCompulsory + " " + this.classTotalHour + " "
				+ this.credit + " " + this.startDate + " " + this.endDate + " "
				+ this.time + " " + this.place + " "
				+ this.assistenceNameString;
	}

	public String toString() {
		return this.courseNumber + " " + this.startDate + " " + this.endDate
				+ " " + this.courseName + " " + this.place + " ";

	}
	public String toTransferString(){
		return this.courseNumber + " " + this.courseName + " "
				+ this.teacherNameString + " " + this.teachersNumString + " "
				+ this.isCompulsory + " " + this.classTotalHour + " "
				+ this.credit + " " + this.startDate + " " + this.endDate + " "
				+ this.time + " " + this.place + " "
				+ this.assistenceNameString;
	}

}
